package com.gxnualbert.proxy.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @auther gxnualbert
 * @create 2021-05-03-16:24
 */
public class ReflectTest {
    public static void main(String[] args) {
        try {

            ReflectServiceImpl target = new ReflectServiceImpl();

            ReflectServiceImpl reflectService = (ReflectServiceImpl) Class.forName("reflect.ReflectServiceImpl").newInstance();
            Method method = reflectService.getClass().getMethod("sayHello", String.class);
            method.invoke(target, "陈晖");
        } catch (NoSuchMethodException | SecurityException | ClassNotFoundException | IllegalAccessException
                | IllegalArgumentException | InstantiationException | InvocationTargetException exception) {
            exception.printStackTrace();
        }
    }

    public ReflectServiceImpl getInstance() {
        ReflectServiceImpl object = null;
        try {
            object = (ReflectServiceImpl) Class.forName("reflect.ReflectServiceImpl").newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException exception) {
            exception.printStackTrace();
        }
        System.out.println(object);
        return object;
    }

    public Object reflectMethod() {
        Object returnObj = null;
        ReflectServiceImpl target = new ReflectServiceImpl();

        try {

            Method method = ReflectServiceImpl.class.getMethod("sayHello", String.class);
            returnObj = method.invoke(target, "陈DDDFD晖");
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException exception) {
            exception.printStackTrace();
        }
        return returnObj;
    }

    public Object reflect() {
        ReflectServiceImpl object = null;

        try {
            object = (ReflectServiceImpl) Class.forName("reflect.ReflectServiceImpl").newInstance();
            Method method = object.getClass().getMethod("sayHello", String.class);
        } catch (NoSuchMethodException | SecurityException | ClassNotFoundException | IllegalAccessException
                | IllegalArgumentException | InstantiationException exception) {
            exception.printStackTrace();
        }
        return object;
    }
}
